namespace Yaaf.WP7.NativeAccess.ComWrapper.AllDevices.COM_Native
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    using Yaaf.WP7.NativeAccess.Helper;

    /// <summary>
    /// Requires COM_Native.dll
    /// </summary>
    internal class NativeDllInterop
    {
        private static Exception ex = null;
        static NativeDllInterop()
        {
            try
            {
                InteropHelper.RegisterDll("COM_Native.dll", "C6BD09B4-96AA-4524-89C4-665A15DD7C9B");
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                ex = e;
            }
        }

        internal static INativeRegistry GetRegistryInterface()
        {
            try
            {
                return new CNativeRegistry() as INativeRegistry;
            }
            catch (Exception e)
            {
                var reg = new RegisterComException("Error while creating interface");
                reg.OtherException = e;
                reg.RegisterException = ex;
                throw reg;
            }
        }

        [ComImport, Guid("C6BD09B4-96AA-4524-89C4-665A15DD7C9B"), ClassInterface(ClassInterfaceType.None)]
        internal class CNativeRegistry
        {
        }

        [ComImport, Guid("34744FD5-ED3B-4418-83E2-B931356F9A4D"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        internal interface INativeRegistry
        {
            [PreserveSig]
            uint GetVersion([MarshalAs(UnmanagedType.LPWStr)] out string result);

            [PreserveSig]
            uint GetSubKeys(string root, string path, [MarshalAs(UnmanagedType.LPWStr)] out string result);

            [PreserveSig]
            uint GetValuesNames(string root, string path, [MarshalAs(UnmanagedType.LPWStr)] out string result);

            [PreserveSig]
            uint GetValueType(string root, string path, string name, out uint result);

            [PreserveSig]
            uint GetDwordValue(string root, string path, string name, out uint result);

            [PreserveSig]
            uint GetStringValue(string root, string path, string name, [MarshalAs(UnmanagedType.LPWStr)] out string result);

            [PreserveSig]
            uint GetMultiStringValue(string root, string path, string name, [MarshalAs(UnmanagedType.LPWStr)] out string result);

            [PreserveSig]
            uint SetDwordValue(string root, string path, string name, uint value);

            [PreserveSig]
            uint SetStringValue(string root, string path, string name, string value);
        }
    }
}